﻿using AngleSharp.Parser.Html;
using Autofac;
using MaximServices.Common;
using MaximServices.Model;
using MaximServices.Services;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace maxim.spider
{
    class Program
    {
        private static MottoServices _mottoServices = ServiceLocator.Instance.Resolve<MottoServices>();
        static void Main(string[] args)
        {
            Begin();

            Console.ReadLine();
        }

        static async Task Begin()
        {
            string beginUrl = "https://tw.18dao.net/%E5%90%8D%E4%BA%BA%E5%90%8D%E8%A8%80/%E9%9B%86%E9%AB%94";
            Dictionary<string, string> allTag = new Dictionary<string, string>();
            allTag.Add("/%E5%90%8D%E4%BA%BA%E5%90%8D%E8%A8%80/%E9%9B%86%E9%AB%94", "集体");
            
            Dictionary<string,string> forWeb  = await GetTagList(await GetBody(beginUrl));
            foreach (var item in forWeb)
            {
                allTag.Add(item.Key, item.Value);
            }
            string baseUrl = "https://tw.18dao.net";
            foreach (var item in allTag)
            {
                try
                {
                    Console.WriteLine($"begin request {item.Value} for url:{item.Key}");
                    string body = await GetBody($"{baseUrl}{item.Key}");
                    Dictionary<string, string> content = await GetContent(body);

                    Console.WriteLine("begin insert");
                    Insert(content, item.Value);
                    Console.WriteLine("insert end");

                    Console.WriteLine("Whether to start the next? y");
                    string input = Console.ReadLine();
                    if (input != "y")
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }

        static async Task<string> GetBody(string url)
        {
            var client = new HttpClient();
            Console.WriteLine("begin request");
            return await client.GetStringAsync(url);
        }

        static string ToSimplified(string Traditional)
        {
            return ChineseConverter.Convert(Traditional, ChineseConversionDirection.TraditionalToSimplified);
        }
        /// <summary>
        /// 获取所有标签和Url
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        static async Task<Dictionary<string,string>> GetTagList(string html)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            try
            {
                var parse = new HtmlParser();
                var doc = await parse.ParseAsync(html);
                var list_a = doc.QuerySelectorAll(@"#mw-content-text p a");
                foreach (var item in list_a)
                {
                    string url = item.GetAttribute("href");
                    string tag = ToSimplified(item.TextContent);
                    if (!result.ContainsKey(url))
                        result.Add(url, tag);
                }
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        static async Task<Dictionary<string,string>> GetContent(string html)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            try
            {
                var parse = new HtmlParser();
                var doc = await parse.ParseAsync(html);
                var list_li = doc.QuerySelectorAll(@"#mw-content-text ol li");
                foreach (var item in list_li)
                {
                    string text = ToSimplified(item.TextContent);
                    string[] sp = text.Split("作者:");
                    if (sp.Length != 2)
                        continue;
                    string con = sp[0].Trim();
                    string auth = sp[1].Trim();
                    Console.WriteLine($"{con} ———— {auth}");
                    if(!result.ContainsKey(con))
                        result.Add(ToSimplified(con), ToSimplified(auth));
                }
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        static void Insert(Dictionary<string, string> list, string tag)
        {
            CreateMottoResultModel model = new CreateMottoResultModel();
            List<CreateMottoDataModel> dataList = new List<CreateMottoDataModel>();
            foreach (var item in list)
            {
                dataList.Add(new CreateMottoDataModel
                {
                    content = item.Key,
                    auth = item.Value,
                    isSuccess = true
                });
            }
            model.DataList = dataList;
            _mottoServices.BatchCreateMotto(model, tag,"自动");
        }

    }
}
